//
//  XTCellOfgeciLrc.m
//  TTLestening
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XTCellOfgeciLrc.h"
#import "NetworkingHandlerOfBlock.h"
#import "DoModel.h"

#import "XCModelForEveryone.h"

@interface XTCellOfgeciLrc ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableview;

@property (nonatomic,strong)NSMutableArray *timearray;

@property (nonatomic,strong)NSMutableDictionary *LRCdic;
@property (nonatomic,assign)NSInteger lrcLineNumber;

@property (nonatomic,assign)NSUInteger current;

@property (nonatomic,assign)NSUInteger zong;

@property (nonatomic,strong)NSArray *array;

@property (nonatomic,assign)NSInteger index;

@end

@implementation XTCellOfgeciLrc

-(instancetype)initWithFrame:(CGRect)frame{
   
    self = [super initWithFrame:frame];
    if (self) {
       
    [self initLRC];
    [self creatview];
       
        
        
    }
    
    return self;
}

-(void)creatview{
    
    self.tableview = [[UITableView alloc]init];
    [self.contentView addSubview:self.tableview];
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    self.tableview.backgroundColor = [UIColor blackColor];
    
 
    
     self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool1"];
    
    

   
    
}



-(void)layoutSubviews{
    
    
    self.tableview.frame = self.contentView.frame;
    
}

-(void)initLRC{
    
    
    NSString *url = @"http://search.dongting.com/lyric/content?sign=5b9aef05376f42765f93d66c65bb9fa6&song_id=332087&lrcid=8905737&type=2&artist=Beyond&title=%E6%B5%B7%E9%98%94%E5%A4%A9%E7%A9%BA&app=ttpod&v=v8.1.5.2016022418&user_id=407738183&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.3&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.8825&longtitude=121.5396&access_token=6fa1fd534a1a5594dea73639eaf7b725";
    
    NSLog(@"%@",url);
    [NetworkingHandler networkingWithUrl:url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        NSString *str = [[result objectForKey:@"data"]objectForKey:@"content"];
        
        
        DoModel * mo = [DoModel initSingleModel];
        NSDictionary * dic = [mo LRCWithName:str];
        self.LRCdic = [NSMutableDictionary dictionaryWithDictionary:[dic  objectForKey:@"LRCDictionary"]];
        
        self.timearray = [NSMutableArray arrayWithArray:[dic objectForKey:@"timeArray"]];
        
        }];
    
     [self displayword];
}


//-(void)initLRC{
//    self.timearray = [NSMutableArray array];
//    self.LRCdic = [NSMutableDictionary dictionary];
//
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"qiushuju" object:nil userInfo:nil];
//
//    
//    
//    [[NSNotificationCenter defaultCenter]addObserverForName:@"1315" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
//        
//        self.array = [note.userInfo objectForKey:@"music"];
//        self.index = [[note.userInfo objectForKey:@"index"] integerValue];
//        
//
//        NSLog(@"qqqqqq%@",self.array);
////=======
////        NSString *str = [[result objectForKey:@"data"]objectForKey:@"content"];
////       // NSLog(@"%@",str);
////>>>>>>> 6ff03d1522499b6adf38a843cb28494f548e0a68
//        
//        NSLog(@"wwwwww%ld",self.index);
//        
//        XCModelForEveryone *model = [self.array objectAtIndex:self.index];
//
//        self.title = model.name;
//        self.song_id = model.song_id;
//        self.artist = model.singerName;
//        
//        NSString *str = model.duration;
//        
//        NSString *str2 = [str substringToIndex:2];
//        
//        
//        NSString *str3 = [str substringFromIndex:3];
//        
//        NSLog(@"%@",str3);
//        
//        
//        NSInteger dua = (str2.integerValue * 60 + str3.integerValue) * 1000;
//        
//        NSLog(@"%@",str2);
//        self.duration = model.duration;
//        
//        NSLog(@"%@",self.title);
//        
//        
//        NSString *url2 = [NSString stringWithFormat:@"http://search.dongting.com/lyric/search?sign=df40c2179c414ce811de44ce51e4aa92&title=%@&song_id=%@&artist=%@&filename=&duration_ms=%ld&app=ttpod",self.title,self.song_id,self.artist,dua];
//        
//        NSLog(@"%@",url2);
//        [NetworkingHandler networkingWithUrl:url2 completeHandle:^(NSURLResponse *response, NSData *data, id result) {
//            
//            NSLog(@"二次解析");
//            NSArray *arr =[result objectForKey:@"data"];
//            
//            
//            NSDictionary *dic = [arr lastObject];
//            
//            self.LRCid = [[dic objectForKey:@"_id"]integerValue];
//            self.lrcType = [[dic objectForKey:@"type"]integerValue];
//            if (self.LRCid != 0 && self.lrcType == 2) {
//                
//                NSString *url = [NSString stringWithFormat:@"http://search.dongting.com/lyric/content?sign=5b9aef05376f42765f93d66c65bb9fa6&song_id=%@&lrcid=%ld&type=2&artist=%@&title=%@&app=ttpod&v=v8.1.5.2016022418",self.song_id,self.LRCid,self.artist,self.title];
//                
//                NSLog(@"%@",url);
//            [NetworkingHandler networkingWithUrl:url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
//                NSString *str = [[result objectForKey:@"data"]objectForKey:@"content"];
//                
//                
//                DoModel * mo = [DoModel initSingleModel];
//                NSDictionary * dic = [mo LRCWithName:str];
//                self.LRCdic = [NSMutableDictionary dictionaryWithDictionary:[dic  objectForKey:@"LRCDictionary"]];
//                
//                self.timearray = [NSMutableArray arrayWithArray:[dic objectForKey:@"timeArray"]];
//                
//                
//                
//                
//                [self displayword];
//
//            }];
//            
//                
//            }else{
//                
//                
//                NSLog(@"没有没有歌词");
//            }
//            
//        }];
//        
//        
//    }];
//    
//}




-(void)displayword{
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"currenTime" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        self.current = [[note.userInfo objectForKey:@"current"] intValue];
        self.zong = [[note.userInfo objectForKey:@"total"] intValue];
        
        DoModel *mo = [DoModel initSingleModel];
        NSUInteger num = self.timearray.count;
        

        for (int i = 0; i < num; i++) {
            NSUInteger currentTime = [mo changeTime:self.timearray[i]];
            
            
            if (i + 1< self.timearray.count) {
                NSUInteger currentTime1 = [mo changeTime:self.timearray[i+1]];
                if (self.current > currentTime && self.current < currentTime1) {
                    
                    [self updateLrcTableView:i];
                }
                
            }else if (self.current > currentTime){//最后一行
                [self updateLrcTableView:i];
            }
            
        }
        
         }];
     [self.tableview reloadData];
    
  }

- (void)updateLrcTableView:(NSUInteger)lineNumber {
    
    self.lrcLineNumber = lineNumber;
   [self.tableview reloadData];
    if (lineNumber > 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lineNumber inSection:0];
        [self.tableview selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];//移动到中心位置
        
//        NSLog(@"运行");
    }
    
    
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.timearray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 35;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.alpha = 0.8;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [self.LRCdic objectForKey:[self.timearray objectAtIndex:indexPath.row]];

    
    if (indexPath.row == self.lrcLineNumber) {
        cell.textLabel.textColor = [UIColor cyanColor];
        cell.textLabel.font = [UIFont fontWithName:@"Arial" size:19];
        cell.textLabel.textAlignment = 1;
        [self.tableview selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    }else{
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.textAlignment = 1;

        
        cell.textLabel.font = [UIFont fontWithName:@"Arial" size:16];
        
    }
    
    
    return cell;
}


@end
